package com.igroupes.ovineherd.model.attribute.enums;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.*;
import com.igroupes.ovineherd.model.attribute.BasicAttribute;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.model.utils.ListUtils;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * 枚举
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class EnumModelData extends BasicAttribute{
    public static final Integer SIZE_MAXAGE = 99999;
    public static final Integer SIZE_MINAGE = 1;
    public static final String SIZE_LABEL = "最大个数";
    public static final String CHOICE_LABEL = "选项集合";
    public static final String CHOICE_DESC = "可选范围";
    private NumberStyle size;
    private EnumStyle choiceList;

    public EnumModelData() {
        super();
        this.size =  new NumberStyle(SIZE_LABEL, SIZE_MINAGE, true,SIZE_MINAGE, SIZE_MAXAGE);
        this.choiceList = new EnumStyle(CHOICE_LABEL,  new ArrayList(),getChoice(),CHOICE_DESC, true);
    }
    private JSONObject getChoice() {
        return new JSONObject(new TextStyle("描述", "", true, 20, ""));
    }
    public Boolean verifyField(EnumModelData modelData) {
        super.verifyField(modelData);
        new NumberStyle().verify(modelData.getSize());
        new EnumStyle().verify(modelData.getChoiceList());
        return true;
    }
    public Boolean verifyFieldV2(EnumModelData basic) {
        super.verifyFieldV2(basic);
        new NumberStyle().verify(new NumberStyle(SIZE_LABEL, basic.getSize().getValue(), true,SIZE_MINAGE, SIZE_MAXAGE));
        new EnumStyle().verify(new EnumStyle(CHOICE_LABEL, basic.getChoiceList().getValue(),getChoice(),CHOICE_DESC, true));
        return true;
    }
    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        EnumModelData modelData = (EnumModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        fieldQuery.setType(MySqlTypeKey.VARCHAR.getCode());
        fieldQuery.setLength(20);

        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        jsonObject.set("size", Convert.toInt(modelData.getSize().getValue()));
        jsonObject.set("choiceList", ListUtils.toListString(modelData.getChoiceList().getValue()));
        dataModelAttributeEntity.setJson(jsonObject);
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries.set(0,fieldQuery);
        return fieldQueries;
    }

    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = super.getAttribute(modelDataJsonAttributeQuery);
        jsonObject.set("size",  new NumberStyle(SIZE_LABEL, modelDataJsonAttributeQuery.getJson().get("size"), true,SIZE_MINAGE, SIZE_MAXAGE));
        jsonObject.set("choiceList", new EnumStyle(CHOICE_LABEL, ListUtils.parseString(Convert.toStr((modelDataJsonAttributeQuery.getJson().get("choiceList")))),this.getChoice(),CHOICE_DESC, true));
        return jsonObject;
    }
    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        super.verifyRequestData(modelAttributeEntity, value);
        List<FieldApiResponse> fieldApiResponses = new ArrayList<>();
        if(value == null){
            return fieldApiResponses;
        }
        List<String> list = (List<String>) value;
        if(modelAttributeEntity.getIsNull().equals(BooleanKey.FALSE.getCode())){
            if(list.size() == 0){
                throw new ModelException(String.format("%s值不能为空,请检测格式是否正确",modelAttributeEntity.getName()));
            }
        }

        JSONObject jsonObject = modelAttributeEntity.getJson();
        if(ObjectUtil.isNotNull(jsonObject.get("size"))){
            Integer size = Convert.toInt(jsonObject.get("size"));

            if(list.size() > size){
                throw new ModelException(String.format("%s数据集合最大不能超过%s",modelAttributeEntity.getName(),size));
            }
        }
//        List<String> choiceList = ListUtils.toListString(jsonObject.get("choiceList"));
//        for (String st: list){
//            if(!choiceList.contains(st)){
//                throw new ModelException(String.format("%s值错误，请确认取值",modelAttributeEntity.getName()));
//            }
//        }
        FieldApiResponse fieldApiResponse = new FieldApiResponse();
        fieldApiResponse.setDbName(modelAttributeEntity.getDbName());
        fieldApiResponse.setValue(ListUtils.toJsonString(list));
        fieldApiResponse.setIsStr(true);
        fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
        fieldApiResponses.add(fieldApiResponse);
        return fieldApiResponses;
    }
    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = super.getAttribute(modelDataJsonAttributeQuery);
        return new JSONObject(new CheckboxStyle(
                modelDataJsonAttributeQuery.getName(),new ArrayList<>(),getGroups(ListUtils.parseString(Convert.toStr(modelDataJsonAttributeQuery.getJson().get("choiceList"))))
                , false, true, Convert.toInt(modelDataJsonAttributeQuery.getJson().get("size"))), false);
    }

    public static List<Group> getGroups(List<String> groups){
        List<Group> groupList = new ArrayList<>();
        for (String str :
                groups) {
            groupList.add(new Group(str, str));
        }
        return groupList;
    }

    @Override
    public Object getData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        if(ObjectUtil.isNotNull(value)){
            return ListUtils.parseString(value.toString());
        }
        return value;
    }
}